<?php
namespace App\Http\Controllers;

use App\Http\Controllers\WebController;
use App\Models\User;
use App\Models\Contact;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Auth;
use Maatwebsite\Excel\Facades\Excel;
use Log;

class IndexController extends WebController
{
    //登录
    public function login() {
        return view('login');
    }
    //提交登录数据
    public function submitLoginData() {
        $phone = $this->request->get('phone');
        $password = $this->request->get('password');
        if(!$password || !$phone) {
            return $this->responseError('请输入帐号和密码');
        }
        $user = User::where('phone', $phone)->where('password', $password)->first();
        if(!$user) {
            $data = ['message'=>'密码错误'];
            return view('login', $data);
        } else {
//            $result= Auth::loginUsingId($user->id);
            return response()->make(redirect()->to('/index'))->withCookie('GNXZ_USER', $user->id);
        }

    }
    //首页
    public function index() {
        return view('index');
    }
    //政策法规
    public function policy() {
        return view('policy');
    }
    //信息公开
    public function information() {
        return view('information');
    }
    //技术服务
    public function technical() {
        return view('technical');
    }
    //下载专区
    public function downloadList() {
        return view('download_list');
    }
    //办事指南
    public function guidance() {
        return view('guidance');
    }
    //新闻一
    public function newsDetail() {
        $id = $this->request->get('id');
        return view('news_detail');
    }
    //下载能源审计
    public function downloadEnergyAudit() {
        return response()->download('download/train-No.6.pdf');
    }
    //下载文件
    public function downloadFileByName() {
        $name = $this->request->get('name');
        if($name) {
            $fileName = 'download/'.$name;
            return response()->download($fileName);
        } else {
            return view('errors.404');
        }
    }
}
